package com.cy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cy.common.entity.Result;
import com.cy.common.util.TransApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/translate")
@Api(tags = "百度翻译模块")
public class TranslationController {
    @Autowired
    private TransApi transApi;

    @ApiOperation("中英互译")
    @GetMapping("/interranslation")
    public Result baiduTranslate(@RequestParam String query, @RequestParam String language) {
        String transResult = transApi.getTransResult(query, "auto", language);
        Map maps = (Map) JSON.parse(transResult);
        String trans_result = maps.get("trans_result").toString();
        String substring = trans_result.substring(1, trans_result.length() - 1);
        Map maps2 = (Map) JSON.parse(substring);
        String dict = maps2.get("dict").toString();
        Map maps3 = (Map) JSON.parse(dict);
        maps2.put("dict",maps3);
        return Result.ok("翻译成功", maps2);
    }
}
